using System;

using System.Web;
using System.Xml;
using CodePlex.OpenSearch;
using System.IO;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace CodePlex.OpenSearch.Example
{
    public class ExampleHandler : SearchHandler
    {
      
        override public bool IsReusable
        {
            get { return true; }
        }


        protected override void Define(SearchDefinition definition)
        {

            definition.ShortName = "Example Web Search";
            definition.LongName = "Example.com Web Search";
            definition.Description = "Use Example.com to search the Web.";
            definition.AddTags("example","web");
            definition.Contact = "admin@example.com";
            definition.Developer = "Example.com Development Team";
            definition.Attribution = "Search data Copyright 2005, Example.com, Inc., All Rights Reserved";
            definition.AdultContent = false;
            definition.SyndicationRight = SyndicationRight.Open;
            definition.AddQuery("example", "cat");



            definition.HttpBase = string.Format("{0}://{1}",
                HttpContext.Current.Request.Url.Scheme,
                HttpContext.Current.Request.Url.Authority
            );

            definition.DescriptionPath = "/description";
            definition.SuggestPath = "/suggest";
            definition.SearchPath = "/search";

            definition.SetIco16("/opensearch16.ico", delegate
            {
                return Util.GetResourceAsStream<ExampleHandler>("Resources.favicon16.ico");
            });

            definition.SetPng64("/opensearch64.png", delegate
            {
                return Util.GetResourceAsStream<ExampleHandler>("Resources.favicon64.png");
            });

        }

        
        override protected IEnumerable<string> GetSuggestions(string searchTerms)
        {
            yield return searchTerms + " books";
            yield return searchTerms + " music";
            yield return searchTerms + " movies";
        }



        protected override SearchResponse Search(HttpContext context, int startPage, string searchTerms) // one-based
        {
            DateTime updated = DateTime.Now;


            List<SearchResult> results = new List<SearchResult>();
            results.Add(new SearchResult("/content/a", "Content A", updated, "An existing page of content"));
            results.Add(new SearchResult("/content/b", "Content B", updated, "An existing page of content"));



            string title = string.Format("Search for '{0}'", searchTerms);
            string description = string.Format("Search results matching the terms {0}", searchTerms);
            string searchAuthorName = "Example.com Development Team";

            int totalResults = 2;
            int startIndex = 1;
            int itemsPerPage = 10;

            int firstPage = 1;
            int previousPage = startPage - 1;
            int nextPage = startPage + 1;
            int lastPage = 1;

            string link = this.GetSearchUrl(Constants.MimeTypes.TextHtml, searchTerms, startPage);

            string searchID = link;

            return new SearchResponse(searchTerms, startPage,

                firstPage, previousPage, nextPage, lastPage,

                title, link, description,
                searchID, searchAuthorName, updated,
                totalResults, startIndex, itemsPerPage, results);
        }





        protected override void ProcessDefault(HttpContext context)
        {
            context.Response.ContentType = Constants.MimeTypes.TextHtml;

            XmlTextWriter writer = new XmlTextWriter(context.Response.Output);
            writer.Formatting = Formatting.Indented;
            writer.WriteStartDocument();

            using (Util.StartElement(writer, "html"))
            {
                using (Util.StartElement(writer, "head"))
                {
                    writer.WriteAttributeString("profile", Constants.Namespaces.OpenSearch);
                    writer.WriteElementString("title", "discover");
                    this.RenderSearchDescriptionAutodiscovery(writer);
                }

                using (Util.StartElement(writer, "body"))
                {
                    writer.WriteElementString("h4", "Content Page");
                    writer.WriteElementString("p", "This page includes the necessary opensearch auto-discovery information.");
                }
            }

            context.Response.End();
        }


        protected override string GetContentType(string preferredContentType)
        {
            Dictionary<string, string> map = new Dictionary<string, string>();
            map[Constants.MimeTypes.ApplicationOpenSearchDescriptionXml] = Constants.MimeTypes.TextXml;
            map[Constants.MimeTypes.ApplicationXSuggestionsJson] = Constants.MimeTypes.TextJavascript;
            map[Constants.MimeTypes.ApplicationRssXml] = Constants.MimeTypes.TextXml;
            map[Constants.MimeTypes.ApplicationAtomXml] = Constants.MimeTypes.TextXml;


            if (map.ContainsKey(preferredContentType))
            {
                return map[preferredContentType];
            }
            else
            {
                return preferredContentType;
            }

          
        }


       
    }
}
